<?php
/*
 * @Author: 天羽落星辰
 * @Date: 2020-08-16 23:04:08
 * @LastEditors: 天羽落星辰
 * @LastEditTime: 2020-08-17 11:29:09
 * @Description: file content
 */

namespace app\index\controller\nozzle;

use app\common\controller\Frontend;
use \app\admin\model\nozzle\Empower as ModelEmpower;

/**
 * 我的授权管理
 */
class Empower extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 表单
     */
    public function index()
    {
        $row = ModelEmpower::where(['user_id' => $this->auth->id])->paginate(10, null);
        $this->view->assign('title', __('我的授权列表'));
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {   
        
        $row = ModelEmpower::get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if($params['white_list_ip']){
                    $user = $this->auth->getUser();
                    $config = get_addon_config('nozzle');
                    $vip_ip_max = $config['vip_ip_max'][$user['level']];
                    // ip数量
                    $ip_count = count(explode(',',$params['white_list_ip']));
                    if($ip_count > $vip_ip_max){
                        $this->error(__('当前白名单最多为 '. $vip_ip_max . '个'));
                    }
                }
                    
                $result = false;
                $result = $row->allowField(true)->save($params);
                if ($result) {
                    $this->success('编辑成功','index');
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->view->assign('title', __('编辑'));
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}
